Computer Science Engineering (CSE) Exam  >  Computer Science Engineering (CSE) Questions  >  Predict the output of following program#inclu... Start Learning for Free
Predict the output of following program
#include <stdio.h>
int f(int n)
{
if(n <= 1)
return 1;
if(n%2 == 0)
return f(n/2);
return f(n/2) + f(n/2+1);
}
int main()
{
printf("%d", f(11));
return 0;
}
  • a)
    Stack Overflow
  • b)
    3
  • c)
    4
  • d)
    5
Correct answer is option 'D'. Can you explain this answer?
Verified Answer
Predict the output of following program#include <stdio.h>int f(i...
On successive recursion F(11) will be decomposed into F(5) + F(6) -> F(2) + F(3) + F(3) -> F(1) + 2 * [F(1) + F(2)] -> 1 + 2 * [1 + F(1)] -> 1 + 2 * (1 + 1) -> 5. Hence , option D is the correct answer i.e, 5.
View all questions of this test
Most Upvoted Answer
Predict the output of following program#include <stdio.h>int f(i...

Explanation:

- The function `f` takes an integer `n` as input and returns an integer.
- If `n` is less than or equal to 1, the function returns 1.
- If `n` is even (i.e., `n%2 == 0`), the function recursively calls itself with `n/2`.
- If `n` is odd, the function recursively calls itself with `n/2` and `n/2 + 1`, and returns the sum of the two recursive calls.

Execution:

- In the `main` function, `f(11)` is called.
- Since 11 is odd, the function `f` is called with `n=11`.
- The function then calls itself with `n/2 = 5` and `n/2 + 1 = 6`.
- For `n=5`, the function calls itself with `n/2 = 2` and returns 1.
- For `n=6`, the function calls itself with `n/2 = 3` and `n/2 + 1 = 4`.
- For `n=3`, the function calls itself with `n/2 = 1` and returns 1.
- For `n=4`, the function calls itself with `n/2 = 2` and returns 1.
- Finally, for `n=6`, the function returns `f(3) + f(4) = 1 + 1 = 2`.
- Therefore, the function `f(11)` returns `f(5) + f(6) = 1 + 2 = 3`.

Therefore, the output of the program will be:
```
3
Explore Courses for Computer Science Engineering (CSE) exam

Top Courses for Computer Science Engineering (CSE)

Predict the output of following program#include <stdio.h>int f(int n){if(n <= 1)return 1;if(n%2 == 0)return f(n/2);return f(n/2) + f(n/2+1);}int main(){printf("%d", f(11));return 0;}a)Stack Overflowb)3c)4d)5Correct answer is option 'D'. Can you explain this answer?
Question Description
Predict the output of following program#include <stdio.h>int f(int n){if(n <= 1)return 1;if(n%2 == 0)return f(n/2);return f(n/2) + f(n/2+1);}int main(){printf("%d", f(11));return 0;}a)Stack Overflowb)3c)4d)5Correct answer is option 'D'. Can you explain this answer? for Computer Science Engineering (CSE) 2024 is part of Computer Science Engineering (CSE) preparation. The Question and answers have been prepared according to the Computer Science Engineering (CSE) exam syllabus. Information about Predict the output of following program#include <stdio.h>int f(int n){if(n <= 1)return 1;if(n%2 == 0)return f(n/2);return f(n/2) + f(n/2+1);}int main(){printf("%d", f(11));return 0;}a)Stack Overflowb)3c)4d)5Correct answer is option 'D'. Can you explain this answer? covers all topics & solutions for Computer Science Engineering (CSE) 2024 Exam. Find important definitions, questions, meanings, examples, exercises and tests below for Predict the output of following program#include <stdio.h>int f(int n){if(n <= 1)return 1;if(n%2 == 0)return f(n/2);return f(n/2) + f(n/2+1);}int main(){printf("%d", f(11));return 0;}a)Stack Overflowb)3c)4d)5Correct answer is option 'D'. Can you explain this answer?.
Solutions for Predict the output of following program#include <stdio.h>int f(int n){if(n <= 1)return 1;if(n%2 == 0)return f(n/2);return f(n/2) + f(n/2+1);}int main(){printf("%d", f(11));return 0;}a)Stack Overflowb)3c)4d)5Correct answer is option 'D'. Can you explain this answer? in English & in Hindi are available as part of our courses for Computer Science Engineering (CSE). Download more important topics, notes, lectures and mock test series for Computer Science Engineering (CSE) Exam by signing up for free.
Here you can find the meaning of Predict the output of following program#include <stdio.h>int f(int n){if(n <= 1)return 1;if(n%2 == 0)return f(n/2);return f(n/2) + f(n/2+1);}int main(){printf("%d", f(11));return 0;}a)Stack Overflowb)3c)4d)5Correct answer is option 'D'. Can you explain this answer? defined & explained in the simplest way possible. Besides giving the explanation of Predict the output of following program#include <stdio.h>int f(int n){if(n <= 1)return 1;if(n%2 == 0)return f(n/2);return f(n/2) + f(n/2+1);}int main(){printf("%d", f(11));return 0;}a)Stack Overflowb)3c)4d)5Correct answer is option 'D'. Can you explain this answer?, a detailed solution for Predict the output of following program#include <stdio.h>int f(int n){if(n <= 1)return 1;if(n%2 == 0)return f(n/2);return f(n/2) + f(n/2+1);}int main(){printf("%d", f(11));return 0;}a)Stack Overflowb)3c)4d)5Correct answer is option 'D'. Can you explain this answer? has been provided alongside types of Predict the output of following program#include <stdio.h>int f(int n){if(n <= 1)return 1;if(n%2 == 0)return f(n/2);return f(n/2) + f(n/2+1);}int main(){printf("%d", f(11));return 0;}a)Stack Overflowb)3c)4d)5Correct answer is option 'D'. Can you explain this answer? theory, EduRev gives you an ample number of questions to practice Predict the output of following program#include <stdio.h>int f(int n){if(n <= 1)return 1;if(n%2 == 0)return f(n/2);return f(n/2) + f(n/2+1);}int main(){printf("%d", f(11));return 0;}a)Stack Overflowb)3c)4d)5Correct answer is option 'D'. Can you explain this answer? tests, examples and also practice Computer Science Engineering (CSE) tests.
Explore Courses for Computer Science Engineering (CSE) exam

Top Courses for Computer Science Engineering (CSE)

Explore Courses
Signup for Free!
Signup to see your scores go up within 7 days! Learn & Practice with 1000+ FREE Notes, Videos & Tests.
10M+ students study on EduRev